#!/usr/bin/env node
let fs = require('fs');
let path = require('path');
let count = 0;

let pathParams = process.cwd(); //当前文件所在的目录路径

function dirTree(pathParams) {
  //判断为目录
  if (fs.statSync(pathParams).isDirectory()) {
    //读取出目录
    console.log(markT(count), getName(pathParams));
    count++;
    let dirsList = fs.readdirSync(pathParams);
    dirsList.forEach(item => {
      //递归
      dirTree(path.join(pathParams, item));
    });
    count--;
  } else {
    //读取出文件
    console.log(markT(count), getName(pathParams));
  }
}


//生成间隔符
function markT(index) {
  if (index === 0) {
    return ' 你要读取的文件:';
  }
  let str = '';
  for (let i = 0; i < index; i++) {
    str += '|--'
  }
  return str;
}

//读取路径中最后的目录名或文件名
function getName(pathParams) {
  return path.parse(pathParams).base;
}

//调用方法读取目录
dirTree(pathParams);
